package com.exartisan.thanhdoanhn;

import java.util.ArrayList;

import com.exartisan.adapter.EventAdapter;
import com.exartisan.bean.Event;
import com.exartisan.network.NetworkUtility;
import com.exartisan.network.ParametersBuilder;
import com.exartisan.network.ResponseTranslater;
import com.exartisan.network.RestConnector;
import com.exartisan.thanhdoanhn.R;
import com.exartisan.util.DataHolder;
import com.loopj.android.http.AsyncHttpResponseHandler;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import android.util.Log;
import android.view.View;
import android.content.Intent;


public class ListEventActivity extends Activity {
	private String[] event_name = { "Sinh Viên Thanh Lịch NUAE",
			"Cửu Long Tranh Bá", "Cửu Âm Chân Kinh" };
	private String[] event_place = {
			"Trường Đại học sư phạm nghệ thuật trung ương", "Facebook.com",
			"Made in china" };
	private String[] num_view = { "1234", "5678", "91011" };
	private String[] event_start = { "00:00 ngày 19/03/2013",
			"00:00 ngày 19/03/2013", "00:00 ngày 19/03/2013" };
	private String[] event_end = { "16:00 ngày 19/03/2013",
			"18:00 ngày 19/03/2013", "09:00 ngày 19/03/2013" };
	ArrayList<Event> listEvents;
	Event event;
ListView lvEvent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_listevent);
		lvEvent=(ListView) findViewById(R.id.lvEvent);
		ShowData();
		lvEvent.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent=new Intent(ListEventActivity.this,DetailEventActivity.class);
				startActivity(intent);
				
			}
		});
		//getData();
	}

	public void ShowData() {
		listEvents = new ArrayList<Event>();

		for (int i = 0; i < event_name.length; i++) {
			event = new Event();
			event.setEventName(event_name[i]);
			event.setPlace(event_place[i]);
			event.setContent(num_view[i]);
			event.setStartDate(event_start[i]);
			event.setEndDate(event_end[i]);
			listEvents.add(event);
		}
		EventAdapter adapter = new EventAdapter(this, 1, listEvents);
		lvEvent.setAdapter(adapter);
	}
	
	private void getData(){
		if (NetworkUtility.checkNetworkState(this)){
			RestConnector.post("", ParametersBuilder.BuildParams(ParametersBuilder.BuildEventData()), new AsyncHttpResponseHandler(){
				@Override
				public void onSuccess(int arg0, String response) {
					super.onSuccess(arg0, response);
					if (ResponseTranslater.checkSuccess(response)){
						listEvents = ResponseTranslater.getEvent(response);
						EventAdapter adapter = new EventAdapter(getBaseContext(), 1, listEvents);
						//setListAdapter(adapter);
					} else {
						Toast.makeText(getBaseContext(), "Không có dữ liệu", Toast.LENGTH_SHORT).show();
						finish();
					}
				}
				
				@Override
				public void onFailure(Throwable arg0, String arg1) {
					super.onFailure(arg0, arg1);
					Log.d("fail", arg1);
					Toast.makeText(getBaseContext(), "Không có dữ liệu", Toast.LENGTH_SHORT).show();
					finish();
				}
			});
		} else {
			Toast.makeText(getBaseContext(), "Không có kết nối", Toast.LENGTH_SHORT).show();
			finish();
		}
	}
	
	/*@Override
	protected void onListItemClick(ListView l, View v, int pos, long id) {
		super.onListItemClick(l, v, pos, id);
		DataHolder.setEvent(listEvents.get(pos));
		Intent intent=new Intent(this,DetailEventActivity.class);
		startActivity(intent);
	}*/

}
